{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "acfb1e99-7282-4976-856b-a4e55ef186ef",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tqdm\n",
    "import numpy as np\n",
    "import time\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "e4275f0b-7d0d-474d-ab18-346cbc8f688f",
   "metadata": {},
   "outputs": [],
   "source": [
    "INIT_SYMBOLS = np.array([[-1, -1, -1, -1, -1],\n",
    "                        [0, -1, 1, -1, 2], \n",
    "                        [-1, 3, -1, 4, -1], \n",
    "                        [-1, -1, -1, -1, -1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "id": "76ffdf8e-1305-4f7d-97e9-c681cd51c24d",
   "metadata": {},
   "outputs": [],
   "source": [
    "MINERAL = [Pearl(), ShinePebble(), Ruby(), Sapphire()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "00d68f67-e556-4d57-a87d-4999af052dab",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "\n",
    "class Symbol:\n",
    "    def __init__(self, name, coin, rare):\n",
    "        self.name = name\n",
    "        self.coin = coin\n",
    "        self.rare = rare\n",
    "        self.MONKEYLIST = ['Banana']\n",
    "    def getCoin(self):\n",
    "        return self.coin\n",
    "\n",
    "    def checkAround(self, syms):\n",
    "        pass\n",
    "\n",
    "class Coin(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Coin, self).__init__('Coin', 1, 1)\n",
    "\n",
    "    def checkAround(self, syms):\n",
    "        pass\n",
    "\n",
    "class Cherry(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Cherry, self).__init__('Cherry', 1, 1)\n",
    "\n",
    "class Pearl(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Pearl, self).__init__('Pearl', 1, 1)\n",
    "class Cat(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Cat, self).__init__('Cat', 1, 1)\n",
    "\n",
    "class Flower(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Flower, self).__init__('Flower', 1, 1)\n",
    "\n",
    "class BananaSkin(Symbol):\n",
    "    def __init__(self):\n",
    "        super(BananaSkin, self).__init__('BananaSkin', 1, 1)\n",
    "\n",
    "class Banana(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Banana, self).__init__('Banana', 1, 1)\n",
    "\n",
    "class Bee(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Bee, self).__init__('Bee', 1, 1)\n",
    "\n",
    "class Beer(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Beer, self).__init__('Beer', 1, 1)\n",
    "\n",
    "class Hurter(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Hurter, self).__init__('Hurter', 1, 1)\n",
    "\n",
    "class Suger(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Suger, self).__init__('Suger', 1, 1)\n",
    "\n",
    "class Cheese(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Cheese, self).__init__('Cheese', 1, 1)\n",
    "\n",
    "class Coal(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Coal, self).__init__('Coal', 1, 1)\n",
    "\n",
    "class Monkey(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Monkey, self).__init__('Monkey', 1, 1)\n",
    "\n",
    "    def checkAround(self, syms):\n",
    "        for i in range(len(syms)):\n",
    "            if syms[i] == None:\n",
    "                    continue\n",
    "            if syms[i] in self.MONKEYLIST: # 周围有香蕉或者椰子\n",
    "                syms[i] = BananaSkin() #替换\n",
    "                VALUE = 7\n",
    "                return VALUE\n",
    "class Miner(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Miner, self).__init__('Miner', 1, 1)\n",
    "\n",
    "    def checkAround(self, syms):\n",
    "            for i in range(len(syms)):\n",
    "                if syms[i] == None:\n",
    "                    continue\n",
    "                if syms[i].name == 'Ore': \n",
    "                    syms[i] = np.random.choice(MINERAL, 1)\n",
    "                    VALUE = 20\n",
    "                    return VALUE\n",
    "\n",
    "\n",
    "class ShinePebble(Symbol):\n",
    "    def __init__(self):\n",
    "        super(ShinePebble, self).__init__('ShinePebble', 1, 1)\n",
    "\n",
    "\n",
    "class Ore(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Ore, self).__init__('Ore', 1, 1)\n",
    "\n",
    "class Ruby(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Ruby, self).__init__('Ruby', 3, 3)\n",
    "\n",
    "class Sapphire(Symbol):\n",
    "    def __init__(self):\n",
    "        super(Sapphire, self).__init__('Sapphire', 2, 2)\n",
    "    \n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "abd27f7a-c416-46dd-b259-2738744e4d00",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Game:\n",
    "    def __init__(self):\n",
    "        self.isEnd = False\n",
    "        self.coin = 1\n",
    "        self.choice_n = 3\n",
    "        self.syms = [Coin(), Pearl(), Cat(), Cherry(), Flower()]\n",
    "        self.RAREITY = ['Common', 'Uncommon', 'Rare', 'Very Rare', 'Special']\n",
    "        self.GRID_SIZE = (4, 5) # 四行五列的网格\n",
    "        self.ALL = [Coin(), Pearl(), Cat(), Cherry(), Flower(), BananaSkin(), Banana(), Bee(), Beer(),\n",
    "                   Hurter(), Suger(), Cheese(), Coal(), Monkey(), Miner(), Ore(), Ruby(), Sapphire(),\n",
    "                   ShinePebble(),]\n",
    "        self.M = INIT_SYMBOLS\n",
    "\n",
    "    def chooseSymbol(self): # 随机选择符号\n",
    "        ls = np.random.choice(self.ALL, self.choice_n, replace=False)\n",
    "        print(\"Add symbol\")\n",
    "        print(f\"(1) {ls[0].name} {self.RAREITY[ls[0].rare-1]}\")\n",
    "        print(f\"(2) {ls[1].name} {self.RAREITY[ls[1].rare-1]}\")\n",
    "        print(f\"(3) {ls[2].name} {self.RAREITY[ls[2].rare-1]}\")\n",
    "        num = input(\"Choose a symbol(input 1, 2, 3, 'skip' or 'exit'):\")\n",
    "        if not num.isdigit():\n",
    "            if num == 'exit':\n",
    "                self.isEnd = True\n",
    "        else:\n",
    "            self.syms.append(ls[int(num)-1])\n",
    "            print(\"Symbol Added !\")\n",
    "\n",
    "    def check(self):\n",
    "        neighbors = self.getNeighbors()\n",
    "        func = lambda x:None if x == -1 or x == None else self.syms[x]\n",
    "        for i in range(len(neighbors)):\n",
    "            row = i // self.GRID_SIZE[1]\n",
    "            col = i % self.GRID_SIZE[1]\n",
    "            self.coin += self.syms[self.M[row, col]].checkAround([func(j) for j in neighbors[i]])\n",
    "            \n",
    "                \n",
    "    \n",
    "    def spin(self):\n",
    "        self.coin -= 1\n",
    "        if len(self.syms) >= 20: # 没有空\n",
    "            self.M = np.random.randint(0, len(self.syms) - 1, self.GRID_SIZE)\n",
    "        else:\n",
    "            matrix = np.full(self.GRID_SIZE, -1)\n",
    "            for i in range(len(self.syms)):\n",
    "                row = np.random.randint(0, 4)\n",
    "                col = np.random.randint(0, 5)\n",
    "                while matrix[row][col] != -1:\n",
    "                    row = np.random.randint(0, 4)\n",
    "                    col = np.random.randint(0, 5)\n",
    "                    \n",
    "                matrix[row][col] = i\n",
    "            self.M = matrix\n",
    "\n",
    "        coin_sum = self.getCoinSum()\n",
    "\n",
    "    def getNeighbors(self):\n",
    "        neighbors = []\n",
    "        for i in range(self.GRID_SIZE[0]):\n",
    "            for j in range(self.GRID_SIZE[1]):\n",
    "                neighbors.append([\n",
    "                    self.M[i-1, j] if i > 0 else None, # 上\n",
    "                    self.M[i+1, j] if i < self.GRID_SIZE[0] - 1 else None, # 下\n",
    "                    self.M[i, j-1] if j > 0 else None,# 左\n",
    "                    self.M[i, j+1] if j < self.GRID_SIZE[1] - 1 else None, #右\n",
    "                    self.M[i-1, j-1] if j > 0 and i > 0 else None,#左上角\n",
    "                    self.M[i-1, j+1] if j < self.GRID_SIZE[1] - 1 and i > 0 else None, # 右上角\n",
    "                    self.M[i+1, j-1] if i < self.GRID_SIZE[0] - 1 and j > 0 else None,# 左下角\n",
    "                    self.M[i+1, j+1] if i < self.GRID_SIZE[0] - 1 and j < self.GRID_SIZE[1] - 1 else None,# 右下角\n",
    "                ])\n",
    "    \n",
    "        return neighbors\n",
    "\n",
    "\n",
    "    def invNeighbor(self, neighbor):\n",
    "        for i in range(self.GRID_SIZE[0]):\n",
    "            for j in range(self.GRID_SIZE[1]):\n",
    "                if j < 1:\n",
    "                    self.M[i, j] = neighbor[i * self.GRID_SIZE[1] + 1][2]\n",
    "                elif j >= self.GRID_SIZE[1] - 1:\n",
    "                    self.M[i, j] = neighbor\n",
    "            \n",
    "\n",
    "    def getCoinSum(self):\n",
    "        sum = 0\n",
    "        for i in self.syms:\n",
    "            sum += i.coin\n",
    "        print(f\"Get Coin {sum} in total\")\n",
    "        self.coin += sum\n",
    "        return sum\n",
    "    def display(self):\n",
    "        for i in self.M:\n",
    "            print(\"\\n\")\n",
    "            for j in i:\n",
    "                if j != -1:\n",
    "                    print(self.syms[j].name.ljust(10), end='  ')\n",
    "                else:\n",
    "                    print('0'.ljust(10), end='  ')\n",
    "        print(\"\\n\")\n",
    "    def start(self):\n",
    "        while not self.isEnd:\n",
    "            print(f\"Coin: {self.coin}\")\n",
    "            time.sleep(2)\n",
    "            self.spin()\n",
    "            self.display()\n",
    "            self.check()\n",
    "            time.sleep(1)\n",
    "            self.chooseSymbol()\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "524e62c5-2c99-4eef-abc6-844a221c884c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Coin: 1\n",
      "5\n",
      "Get Coin 5 in total\n",
      "\n",
      "\n",
      "0           0           0           0           Cherry      \n",
      "\n",
      "0           0           0           Pearl       0           \n",
      "\n",
      "0           Cat         0           0           Coin        \n",
      "\n",
      "0           0           Flower      0           0           \n",
      "\n",
      "Add symbol\n",
      "(1) Ruby Rare\n",
      "(2) Monkey Common\n",
      "(3) Hurter Common\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Choose a symbol(input 1, 2, 3, 'skip' or 'exit'): 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Symbol Added !\n",
      "Coin: 5\n",
      "6\n",
      "Get Coin 8 in total\n",
      "\n",
      "\n",
      "0           Ruby        0           0           Pearl       \n",
      "\n",
      "0           0           Cat         Coin        0           \n",
      "\n",
      "0           0           0           0           0           \n",
      "\n",
      "Cherry      0           Flower      0           0           \n",
      "\n",
      "Add symbol\n",
      "(1) Monkey Common\n",
      "(2) Ruby Rare\n",
      "(3) Suger Common\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Choose a symbol(input 1, 2, 3, 'skip' or 'exit'): 1\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Symbol Added !\n",
      "Coin: 12\n",
      "7\n",
      "Get Coin 9 in total\n",
      "\n",
      "\n",
      "0           Flower      0           Ruby        0           \n",
      "\n",
      "0           0           0           0           0           \n",
      "\n",
      "0           0           Monkey      0           Coin        \n",
      "\n",
      "Cat         Cherry      0           Pearl       0           \n",
      "\n",
      "Add symbol\n",
      "(1) Pearl Common\n",
      "(2) Banana_skin Common\n",
      "(3) Bee Common\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Choose a symbol(input 1, 2, 3, 'skip' or 'exit'): 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Symbol Added !\n",
      "Coin: 20\n",
      "8\n",
      "Get Coin 10 in total\n",
      "\n",
      "\n",
      "Bee         Coin        0           0           0           \n",
      "\n",
      "0           Cat         0           Ruby        0           \n",
      "\n",
      "0           0           0           Pearl       Cherry      \n",
      "\n",
      "0           0           Monkey      Flower      0           \n",
      "\n",
      "Add symbol\n",
      "(1) Coal Common\n",
      "(2) Flower Common\n",
      "(3) Ore Common\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Choose a symbol(input 1, 2, 3, 'skip' or 'exit'): 3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Symbol Added !\n",
      "Coin: 29\n",
      "9\n",
      "Get Coin 11 in total\n",
      "\n",
      "\n",
      "0           0           Cat         Cherry      0           \n",
      "\n",
      "0           0           Bee         Monkey      Ruby        \n",
      "\n",
      "Flower      0           Ore         0           0           \n",
      "\n",
      "0           0           0           Coin        Pearl       \n",
      "\n",
      "Add symbol\n",
      "(1) Miner Common\n",
      "(2) Bee Common\n",
      "(3) Coin Common\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "Choose a symbol(input 1, 2, 3, 'skip' or 'exit'): exit\n"
     ]
    }
   ],
   "source": [
    "g1 = Game()\n",
    "\n",
    "g1.start()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
